package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldTrainIecturer;
import com.cleaningcloud.cld.domain.dto.CldTrainIecturerDto;
import com.cleaningcloud.cld.domain.dto.CldTrainPlanDto;
import com.cleaningcloud.cld.domain.dto.CldTrainReceiveDto;
import com.cleaningcloud.cld.domain.vo.CldTrainIecturerVo;
import com.cleaningcloud.cld.domain.vo.CldTrainPlanVo;
import com.cleaningcloud.cld.domain.vo.CldTrainReceiveVo;
import com.cleaningcloud.cld.mapper.CldTrainIecturerMapper;
import com.cleaningcloud.cld.mapper.CldTrainReceiveMapper;
import com.cleaningcloud.cld.service.ICldTrainIecturerService;
import com.cleaningcloud.cld.service.ICldTrainPlanService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 培训讲师Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-22
 */
@Service
public class CldTrainIecturerServiceImpl implements ICldTrainIecturerService {
    @Autowired
    private CldTrainIecturerMapper cldTrainIecturerMapper;

    @Autowired
    private CldTrainReceiveMapper cldTrainReceiveMapper;

    @Autowired
    private ICldTrainPlanService trainPlanService;

    /**
     * 查询培训讲师
     *
     * @param trainIecturerId 培训讲师主键
     * @return 培训讲师
     */
    @Override
    public CldTrainIecturerVo getTrainIecturerStaffListInfo(CldTrainReceiveDto cldTrainReceiveDto) {
        CldTrainIecturerVo cldTrainIecturerVo = cldTrainIecturerMapper.selectCldTrainIecturerByTrainIecturerId(cldTrainReceiveDto.getTrainIecturerId());
        // 查询受训人列表
        cldTrainReceiveDto.setTrainIecturerId(cldTrainIecturerVo.getStaffId());
        List<CldTrainReceiveVo> cldTrainReceiveVos = cldTrainReceiveMapper.selectCldTrainReceiveList(cldTrainReceiveDto);
        cldTrainIecturerVo.setTrainReceiveList(cldTrainReceiveVos);
        return cldTrainIecturerVo;
    }

    /**
     * 查询培训讲师列表
     *
     * @param cldTrainIecturer 培训讲师
     * @return 培训讲师
     */
    @Override
    public List<CldTrainIecturerVo> selectCldTrainIecturerList(CldTrainIecturerDto cldTrainIecturer) {
        return cldTrainIecturerMapper.selectCldTrainIecturerList(cldTrainIecturer);
    }

    /**
     * 新增培训讲师
     *
     * @param cldTrainIecturer 培训讲师
     * @return 结果
     */
    @Override
    public int insertCldTrainIecturer(CldTrainIecturer cldTrainIecturer) {
        CldTrainIecturer trainIecturer = cldTrainIecturerMapper.selectCldTrainIecturerByStaffId(cldTrainIecturer.getStaffId());
        if (trainIecturer != null) {
            throw new RuntimeException("该人员已是培训员,请勿重复添加！");
        }
        return cldTrainIecturerMapper.insertCldTrainIecturer(cldTrainIecturer);
    }

    /**
     * 修改培训讲师
     *
     * @param cldTrainIecturer 培训讲师
     * @return 结果
     */
    @Override
    public int updateCldTrainIecturer(CldTrainIecturer cldTrainIecturer) {
        return cldTrainIecturerMapper.updateCldTrainIecturer(cldTrainIecturer);
    }

    /**
     * 批量删除培训讲师
     *
     * @param trainIecturerIds 需要删除的培训讲师主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainIecturerByTrainIecturerIds(Long[] trainIecturerIds) {
        return cldTrainIecturerMapper.deleteCldTrainIecturerByTrainIecturerIds(trainIecturerIds);
    }

    /**
     * 删除培训讲师信息
     *
     * @param trainIecturerId 培训讲师主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainIecturerByTrainIecturerId(Long trainIecturerId) {
        return cldTrainIecturerMapper.deleteCldTrainIecturerByTrainIecturerId(trainIecturerId);
    }

    @Override
    public int delCancePosition(Long trainIecturerId) {
        // 根据培训员查询培训计划
        CldTrainPlanDto query = new CldTrainPlanDto();
        query.setTrainStatus(1L);
        query.setTrainIecturerId(trainIecturerId);
        List<CldTrainPlanVo> list = trainPlanService.selectCldTrainPlanList(query);
        if (!list.isEmpty()) {
            throw new RuntimeException("存在进行中的培训计划,无法进行取消任职！");
        }
        CldTrainIecturer cldTrainIecturer = cldTrainIecturerMapper.selectCldTrainIecturerByTrainIecturerId(trainIecturerId);
        cldTrainIecturer.setUnIecturerTime(DateUtils.getNowDate());
        cldTrainIecturer.setStatus(0L);
        cldTrainIecturer.setCreateId(SecurityUtils.getUserId());
        return cldTrainIecturerMapper.updateCldTrainIecturer(cldTrainIecturer);
    }
}
